package com.atguigu.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadFileStreamRequest;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadURLStreamRequest;
import com.aliyun.vod.upload.resp.UploadFileStreamResponse;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod.upload.resp.UploadURLStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.commonutils.R;
import com.atguigu.service.VodService;
import com.atguigu.servicebase.handler.MyException;
import com.atguigu.utils.AliyunVideoConstont;
import com.atguigu.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther: fangshicai
 * @Date: 2022/2/25 16:14
 * @Description:
 */
@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadAliyunVideo(MultipartFile file) {

        System.out.println(file);
        try {
            //inputStream 以输入流的形式
            InputStream  inputStream = file.getInputStream();
            //fileName 是文件的名字
            String fileName = file.getOriginalFilename();
            //title 是在阿里云里的名字
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //accessKeyId, accessKeySecret 已经在工具类中定义 所以直接在工具类中引用
            UploadStreamRequest request = new UploadStreamRequest(AliyunVideoConstont.ACCESS_KEY_ID, AliyunVideoConstont.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            if (response.isSuccess()) {
                return response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                return response.getVideoId();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }



    }

    @Override
    public void removeAliyunVideoBatch(List<String> videoAliyunSourseIdsList) {
        try {
            //1.创建初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(AliyunVideoConstont.ACCESS_KEY_ID,AliyunVideoConstont.ACCESS_KEY_SECRET);
            //2.创建request
            DeleteVideoRequest request = new DeleteVideoRequest();

            //把videoAliyunSourseIds 转换为 11, 22, 33 的结构
            String ids = StringUtils.join(videoAliyunSourseIdsList, ",");
            //3.创建request设置视频id
            request.setVideoIds(ids);
            //4.创建response
            //5.删除
            defaultAcsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw  new MyException(20001,"删除失败");
        }
    }
}
